/**
 * Created by zhanglihe on 15/11/7.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog',
    'datepicker'
], function( $, common, ready, Collection, template, Model, dialog,datepicker ) {
    var index = function() {
        this.init();
    };
    index.prototype = {
        init: function() {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function() {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'activities',
                model: {
                    url:_global.url.api + 'activity',
                    event: {
                        '.item-edit:click': 'edit',
                        '.item-remove:click': 'remove',
                    },
                    edit: function(e) {
                        _this.data = this.data;
                        var model = this;
                        e.preventDefault();
                        _this.dialog = dialog({
                            title:'活动修改',
                            content: template('add-tpl', this.data),
                            width: 600,
                            ok: function() {
                                _this.data.image = $("input[name='imgs']").val();
                                if(_this.data.image==''||_this.data.image==undefined){
                                    Alert({msg:'请上传活动图片',type:'error'});
                                    return false;
                                }
                                _this.data.name = $('#name').val();
                                if(_this.data.name==''){
                                    Alert({msg:'请填写活动名称',type:'error'});
                                    return false;
                                }
                                _this.data.share_title = $('#share_title').val();
                                _this.data.share_desc = $('#share_desc').val();
                                _this.data.intro = $('#intro').val();

                                if(_this.data.share_title==''){
                                    Alert({msg:'分享标题不能为空',type:'error'});
                                    return false;
                                }
                                if(_this.data.share_desc==''){
                                    Alert({msg:'分享内容不能为空',type:'error'});
                                    return false;
                                }
                                _this.data.raffle_id=$('#raffle_id').val();
                                _this.data.status = 1;
                                _this.data.id = _this.data.id;
                                if(model.put()){
                                    _this.collection.reload();
                                };
                            },cancel: function() {}
                        });
                        _this.dialog.show();
                        $('input,select,textarea').on('change', function(e) {
                            _this.change(e);
                        });
                    },
                    remove: function(e) {
                        e.preventDefault();
                        var model = this;
                        dialog({
                            'content': '确定删除吗?',
                            ok: function() {
                                if(model.del()){
                                    _this.collection.reload();
                                };
                            }, cancel: function() {}
                        }).show(e.target);
                    },
                },
                callback: function() {
                    loaded();
                }
            });
            this.collection.fetch();
        },

        event: function() {
            this.data = {};
            var _this = this;
            //保存
            $('#btn-add').on('click', function(e) {
                _this.data = {};
                e.preventDefault();
                _this.dialog = dialog({
                    title:'添加活动',
                    content: template('add-tpl', {}),
                    width: 600,
                    ok: function() {
                        _this.data.image = $("input[name='imgs']").val();
                        if(_this.data.image==''||_this.data.image==undefined){
                            Alert({msg:'请上传活动图片',type:'error'});
                            return false;
                        }
                        _this.data.name = $('#name').val();
                        if(_this.data.name==''){
                            Alert({msg:'请填写活动名称',type:'error'});
                            return false;
                        }
                        _this.data.intro = $('#intro').val();
                        _this.data.share_title = $('#share_title').val();
                        _this.data.share_desc = $('#share_desc').val();

                        if(_this.data.share_title==''){
                            Alert({msg:'请填写分享标题',type:'error'});
                            return false;
                        }
                        if(_this.data.share_desc==''){
                            Alert({msg:'请填写分享内容',type:'error'});
                            return false;
                        }

                        _this.data.status = 1;
                        _this.data.id = _this.data.id;

                        if(new Model({
                                url: _global.url.api + 'activity',
                                data: _this.data
                            }).post()) {
                            _this.collection.reload();
                        }
                    },cancel: function() {}
                });
                _this.dialog.show();
                $('input,select,textarea').on('change', function(e) {
                    _this.change(e);
                });
            });

            //上传图片
            $('body').on('click', '#btn-upload', function(e) {
                _this.upload_dialog = dialog({
                    content: template('upload-tpl', {}),
                    width: 550,
                    ok: function() {
                        var img = $("#upload-frame")[0].contentWindow.image.get();
                        _this.imgPreview(img);
                    }, cancel: function() {}
                });
                _this.upload_dialog.show();
            });
            $('#btn-search').on('click', function(e) {
                e.preventDefault();
                _this.collection.condition.keyword = $('input[name="keyword"]').val();
                _this.collection.reload();
            });
        },

        change: function(e) {
            this.data[$(e.target).attr('name')] = $(e.target).val();
        },

        imgPreview: function(img) {
            if(typeof img.id !== 'undefined') {
                this.data.img = img.id;
                $('#img-view').html(template('img-tpl', img));
            }
        }
    };

    new index();

});